package com.example.scaffold.organization.controller;

import com.example.scaffold.common.bean.PageResult;
import com.example.scaffold.common.result.Result;
import com.example.scaffold.organization.bean.OrganizationDTO;
import com.example.scaffold.organization.bean.OrganizationEditParam;
import com.example.scaffold.organization.bean.OrganizationQueryParam;
import com.example.scaffold.organization.bean.OrganizationVO;
import com.example.scaffold.organization.service.OrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * 机构管理控制器
 * 遵循阿里手册：控制器命名规范，接口设计规范
 *
 * @author example
 */
@RestController
@RequestMapping("/organization")
@Tag(name = "机构管理", description = "机构相关操作接口")
@RequiredArgsConstructor
public class OrganizationController {
    private static final Logger log = LoggerFactory.getLogger(OrganizationController.class);
    
    private final OrganizationService organizationService;

    /**
     * 根据ID查询机构
     *
     * @param id 机构ID
     * @return 机构信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询机构", description = "通过机构ID获取机构详细信息")
    public Result<OrganizationDTO> getOrganizationById(
            @Parameter(description = "机构ID", required = true)
            @PathVariable Long id) {
        log.info("查询机构信息，机构ID:{}", id);
        return Result.success(organizationService.getOrganizationById(id));
    }

    /**
     * 根据属性查询机构，带有分页
     */
    @PostMapping("/page")
    @Operation(summary = "根据属性查询机构", description = "根据属性查询机构，带有分页")
    public Result<PageResult<OrganizationVO>> listOrganization(@RequestBody OrganizationQueryParam param) {
        log.info("根据属性查询机构，参数:{}", param);
        return Result.success(organizationService.listOrganization(param));
    }

    /**
     * 创建机构
     *
     * @param param 机构信息
     * @return 操作结果
     */
    @PostMapping
    @Operation(summary = "创建机构", description = "新增机构信息")
    public Result<Long> createOrganization(
            @Parameter(description = "机构信息", required = true)
            @Valid @RequestBody OrganizationEditParam param) {
        log.info("创建机构，机构名称:{}", param.getName());
        Long orgId = organizationService.createOrganization(param);
        log.info("创建机构成功，机构ID:{}", orgId);
        return Result.success(orgId);
    }

    /**
     * 更新机构
     *
     * @param id    机构ID
     * @param param 机构信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新机构", description = "修改机构信息")
    public Result<Boolean> updateOrganization(
            @Parameter(description = "机构ID", required = true)
            @PathVariable Long id,
            @Parameter(description = "机构信息", required = true)
            @Valid @RequestBody OrganizationEditParam param) {
        log.info("更新机构，机构ID:{}", id);
        return Result.success(organizationService.updateOrganization(id, param));
    }

    /**
     * 删除机构
     *
     * @param id 机构ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除机构", description = "根据ID删除机构")
    public Result<Boolean> deleteOrganization(
            @Parameter(description = "机构ID", required = true)
            @PathVariable Long id) {
        log.info("删除机构，机构ID:{}", id);
        return Result.success(organizationService.deleteOrganization(id));
    }
}